package utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;

import android.util.Log;

public class FileHelper
{
	private String path;
		
	public FileHelper (String path)
	{
		this.path = path;
	}
	
	public boolean grabar (String id)
	{
		try
		{
			File file = new File(path);
			PrintWriter pw = new PrintWriter(file);
			
			pw.print(id);
			pw.close();
			
			return true;
		}
		catch (Exception e)
		{
			Log.i("LOG", e.getMessage());
			return false;
		}
	}
	
	public long leerLong ()
	{
		try
		{
			return Long.parseLong(leer());
		}
		catch (NumberFormatException ex)
		{
			return -1;
		}
	}
	
	public String leer ()
	{
		try
		{
			File file = new File (path);
			
			if (!file.exists())
				return null;
			
			BufferedReader br = new BufferedReader(new FileReader(file));
			
			String linea = br.readLine();

			br.close();
			
			return linea;
		}
		catch (Exception e)
		{
			Log.i("LOG", e.getMessage());
			return null;
		}
	}
	
	public boolean delete ()
	{
		File file = new File(path);
		
		if (file.exists())
			return file.delete();
		
		return false;
	}
}
